//
//  MiddleController.m
//  OurMenu
//
//  Created by lanou3g on 15/10/10.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "MiddleController.h"
#import "AWCollectionViewDialLayout.h"
#import "DownLoadDataHelper.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "IANshowLoading.h"
#import "DetailsViewController.h"
@interface MiddleController ()

@end

@implementation MiddleController
{
    AWCollectionViewDialLayout *dialLayout;
    UISlider *radiusSlider;
    UISlider *angularSpacingSlider;
    UISlider *xOffsetSlider;
    UIView *settingsView;
    UILabel *angularSpacingLabel;
    UILabel *radiusLabel;
    UICollectionView *collectionView;
    NSMutableDictionary *thumbnailCache;
    NSString *urlStr;
    DownLoadDataHelper *down;
    NSString  *NowStr;
    NSInteger number;
    //NSString *str;
    UIView *loadingView;
    UIImageView *collectionimage;
}
@synthesize items;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.str;
    self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    down = [[DownLoadDataHelper alloc] init];
    down.sendDataDelegete = self;
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=2392dbacea9127e5beb16b0758c3fcaa&menu=%@&rn=10&pn=3",self.str];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareBar)];
//    UIImageView *bacView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
//    [bacView setImage:[UIImage imageNamed:@"qqq"]];
//    [self.view addSubview:bacView];
    [down downLoadDataWithUrlString:NowStr];
    self.dataArray = [NSMutableArray array];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)collectionViewLayout:layout];
    //加载视图
    collectionimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 108)];
    collectionimage.image = [UIImage imageNamed:@"115"];
    [self.view addSubview:collectionimage];
    collectionimage.userInteractionEnabled = YES;
    collectionimage.alpha = 0.5;

    
    [self.view addSubview:collectionView];
    [IANshowLoading showGrayLoadingForView:collectionimage allowUserInteraction:YES];
    
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerNib:[UINib nibWithNibName:@"dialCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cellId"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView addFooterWithTarget:self action:@selector(dragUp)];
    
    
    settingsView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-44)];
    [settingsView setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.6]];
    [self.view addSubview:settingsView];
    [self buildSettings];
    
    
    CGFloat radius = radiusSlider.value * 1000;
    CGFloat angularSpacing = angularSpacingSlider.value * 90;
    CGFloat xOffset = xOffsetSlider.value * 320;
    CGFloat cell_width = 240;
    CGFloat cell_height = 100;
    
    number = 1;
    
    dialLayout = [[AWCollectionViewDialLayout alloc] initWithRadius:radius andAngularSpacing:angularSpacing andCellSize:CGSizeMake(cell_width, cell_height) andAlignment:WHEELALIGNMENTCENTER andItemHeight:cell_height andXOffset:xOffset];
    [collectionView setCollectionViewLayout:dialLayout];
    
    
    [self switchExample];
    
}
 - (void)shareBar
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"561df58d67e58e82e1000a51"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"1.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQzone,UMShareToQQ, nil]
                                       delegate:self];
    
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}

- (void)sendDataBack:(NSData *)data isRequest:(BOOL)isRequest
{
    if (isRequest) {
        
        NSDictionary *allDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *towDic = [allDic objectForKey:@"result"];
        NSArray *allArray = [towDic objectForKey:@"data"];
        for (NSDictionary *dic in allArray) {
            DishesMadels *dishes = [[DishesMadels alloc] init];
            [dishes setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:dishes];
        }
        [collectionView reloadData];
        [IANshowLoading hideLoadingForView:collectionimage];
        
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"网络连接失败" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
            [self dismissViewControllerAnimated:YES completion:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        [alert addAction:action ];

    }
}


-(void)buildSettings{
    NSArray *viewArr = [[NSBundle mainBundle] loadNibNamed:@"iphone_settings_view" owner:self options:nil];
    UIView *innerView = [viewArr objectAtIndex:0];
    CGRect frame = innerView.frame;
    frame.origin.y = (self.view.frame.size.height/2 - frame.size.height/2)/2;
    innerView.frame = frame;
    [settingsView addSubview:innerView];
    
    radiusLabel = (UILabel*)[innerView viewWithTag:100];
    radiusSlider = (UISlider*)[innerView viewWithTag:200];
    angularSpacingSlider = (UISlider*)[innerView viewWithTag:201];
    xOffsetSlider = (UISlider*)[innerView viewWithTag:202];
    
    
}

- (void)dragUp
{
    number = (number++) * 10 + 1;
    NSString *string = [NSString stringWithFormat:@"%ld",number];
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=2392dbacea9127e5beb16b0758c3fcaa&menu=%@&rn=20&pn=%@",self.str,string];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [down downLoadDataWithUrlString:NowStr];
    [collectionView footerEndRefreshing];
    
}



-(void)switchExample{
    
    CGFloat radius = 0 ,angularSpacing  = 0, xOffset = 0;
    // 滚动图片大小
    [dialLayout setCellSize:CGSizeMake(240, 100)];
    [dialLayout setWheelType:WHEELALIGNMENTLEFT];
    radius = 300;
    angularSpacing = 18;
    xOffset = 70;
    [collectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell;
    cell = [cv dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    NSDictionary *item = [self.dataArray objectAtIndex:indexPath.row];
    NSString *playerName = [item valueForKey:@"title"];
    UILabel *nameLabel = (UILabel*)[cell viewWithTag:101];
    nameLabel.font = [UIFont systemFontOfSize:18];
    //nameLabel.textColor = [UIColor whiteColor];
    nameLabel.numberOfLines = 0;
    [nameLabel setText:playerName];
    
    UIView *borderView = [cell viewWithTag:102];
    borderView.layer.borderWidth = 1;
    NSString *imgURL = [[item valueForKey:@"albums"] lastObject];
    UIImageView *imgView = (UIImageView*)[cell viewWithTag:100];
    [imgView setImage:nil];
    
    
    imgView.image = [UIImage imageNamed:imgURL];
    [imgView sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"1"]];
    return cell;
}





-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detail = [[DetailsViewController alloc] init];
    detail.dish = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:detail animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
